package com.kye.express.business.jd.address;

import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.IView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.jd.address.request.RequestAddModifyAddress;
import com.kye.model.bean.user.User;
import com.kye.model.datasource.JDShopDataSource;

import io.reactivex.Single;
import io.reactivex.SingleSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 31/01/2018
 * @updated : 31/01/2018
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class JDAddressAddEditConstract {

    interface JDAddressAddEditView extends IView {

        void onUser(User user);

        void onLoading();

        void onLoaded();

        void saveAddressSuccess();

        void saveAddressServerFailed(String msg);

        void saveAddressNetFailed(String message);

    }

    public static class JDAddressAddEditPresenter extends BasePresenter<JDAddressAddEditConstract.JDAddressAddEditView> {

        private JDShopDataSource jdShopDataSource;

        public JDAddressAddEditPresenter() {
            jdShopDataSource = new JDShopDataSource();
        }

        @Override
        public void bind(JDAddressAddEditView view) {
            super.bind(view);
            UserSession.getInstance()
                    .getUserForRx()
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<User>() {
                        @Override
                        public void accept(User user) throws Exception {
                            getView().onUser(user);
                        }
                    });
        }

        public void checkBeforeSave() {

        }

        public void saveAddress(RequestAddModifyAddress requestAddModifyAddress) {
            Single.just(requestAddModifyAddress)
                    .flatMap(new Function<RequestAddModifyAddress, SingleSource<OpenApiResult<Object>>>() {
                        @Override
                        public SingleSource<OpenApiResult<Object>> apply(RequestAddModifyAddress requestAddModifyAddress) throws Exception {
                            return jdShopDataSource.addModifyAddress(requestAddModifyAddress)
                                    .compose(getView().bindToLifecycleDestroy());
                        }
                    })
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<Object>() {
                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onSuccess(OpenApiResult<Object> result, Object o) {
                            getView().saveAddressSuccess();
                        }

                        @Override
                        public void onApiError(OpenApiResult<Object> result, int code, String msg) {
                            getView().saveAddressServerFailed(msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().saveAddressNetFailed(e.getMessage());
                        }

                        @Override
                        public void onLoaded() {
                            getView().onLoaded();
                        }
                    });
        }
    }
}
